package com.xd.cloud.service.device;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xd.cloud.common.util.DateUtil;
import com.xd.cloud.dao.Page;
import com.xd.cloud.dao.device.DeviceDao;
import com.xd.cloud.dao.device.DeviceSimDao;
import com.xd.cloud.dao.device.SimDao;
import com.xd.cloud.entity.device.Device;
import com.xd.cloud.entity.device.DevicePo;
import com.xd.cloud.entity.device.DeviceSim;
import com.xd.cloud.entity.device.Sim;

/**   
 * 功能描述：设备信息service
 * @author: wangshanshan   
 * @date: 2017年12月11日 上午10:26:27 
 */
@Service
public class DeviceService {

	@Autowired
	private DeviceDao deviceDao;
    @Autowired
    private SimDao simDao;
    @Autowired
    private DeviceSimDao deviceSimDao;
	
	public List<Device> getAllDevice(int pageNo, int pageSize) {
		Page dataPage = deviceDao.getAllDevice(pageNo, pageSize);
		if(dataPage!=null) {
			List<Device> retuList = new ArrayList<Device>();
			List<Object[]> dataList = dataPage.getResult();
			if (dataList != null) {
				for (Object[] item:dataList){
					DevicePo obj = new DevicePo();
					obj.setId((Integer)item[0]);
					obj.setDevice_id((String)item[1]);
					if(obj.getDevice_id().length()>25) {
						obj.setDevice_id_substr(obj.getDevice_id().substring(0, 25)+"...");
					}else {
						obj.setDevice_id_substr(obj.getDevice_id());
					}
					obj.setSerial_id((String)item[2]);
					//obj.setDevice_type((int)item[3]);
					obj.setDevice_type_name((String)item[4]);
					obj.setBrand((String)item[5]);
					obj.setModel((String)item[6]);
					obj.setInstall((Integer)item[7]);
					obj.setInstall_name(obj.getInstall()==0?"未安装":"已安装");
					if(item[8]==null) {
						obj.setInstall_time_str("");
					}else {
						obj.setInstall_time((Date)item[8]);
						obj.setInstall_time_str(DateUtil.DateToString(DateUtil.format_YYYY_MM_DD_HH_MM_SS, obj.getInstall_time()));
					}
					//obj.setCompany_id((int)item[9]);
					obj.setCompany_name((String)item[10]);
					obj.setVehid((String)(item[11]==null?"":item[11]));
					Object[] simObj = (Object[]) simDao.getSimObjectByDeviceId(obj.getId());
					if(simObj!=null) {
						obj.setSim_id((Integer)simObj[0]);
						obj.setSim_card((String)simObj[1]);
					}
					retuList.add(obj);
				}
		    }
			return retuList;
		}
		return null;
	}
	
	public Device getDeviceById(int id) {
		return deviceDao.getDeviceById(id);
	}
	
	public Device getDeviceByDeviceId(String device_id) {
		return deviceDao.getDeviceByDeviceId(device_id);
	}
	
	public Device getDeviceObjectById(int id){
		Object[] item = (Object[]) deviceDao.getDeviceObjectById(id);
		if(item!=null) {
			DevicePo obj = new DevicePo();
			obj.setId((Integer)item[0]);
			obj.setDevice_id((String)item[1]);
			obj.setSerial_id((String)item[2]);
			obj.setDevice_type((Integer)item[3]);
			obj.setBrand((String)item[4]);
			obj.setModel((String)item[5]);
			obj.setInstall((Integer)item[6]);
			obj.setInstall_time((Date)item[7]);
			obj.setFactory_type((Integer)item[8]);
			obj.setSoftware_ver((String)item[9]);
			obj.setHardware_ver((String)item[10]);
			obj.setProduct_manufacturer((String)item[11]);
			obj.setRemark((String)item[12]);
			obj.setCompany_id((Integer)(item[13]==null?-1:item[13]));
			obj.setCompany_name((String)item[14]);
			Object[] simObj = (Object[]) simDao.getSimObjectByDeviceId(obj.getId());
			if(simObj!=null) {
				obj.setSim_id((Integer)simObj[0]);
				obj.setSim_card((String)simObj[1]);
			}
			return obj;
		}
		return null;
	}
	
	public boolean save(Device item){
		deviceDao.save(item);
		return true;
	}
	
	public boolean update(Device item){
		deviceDao.update(item);
		return true;
	}
	
	public boolean deleteInfo(int id){
		Device item = getDeviceById(id);
		if(item!=null) {
			int deviceId = item.getId();
			deviceDao.delete(item);  //删除设备基本信息
			DeviceSim deviceSim = deviceSimDao.getDeviceSimByDeviceId(deviceId);
			if(deviceSim!=null) {//若此设备有卡关联， 删除设备-sim卡,更新此卡的安装状态
				int simId = deviceSim.getSim_id();
				simDao.updateSimInstall(simId, 0);
			}
			return true;
		}else{
			return false;
		}
	}
	
	public List<Device> getDeviceList(int install)
	{
		return deviceDao.getDeviceList(install);
	}
	
	public Device getDeviceBydeviceid(String deviceId)
	{
		return deviceDao.getDeviceBydeviceid(deviceId);
	}
	
	//保存 设备-sim卡关系,操作成功后 更新一下sim卡的安装状态  已安装
	public boolean saveDeviceSim(int device_id, int sim_id) {
		try {
			DeviceSim item = new DeviceSim();
			item.setDevice_id(device_id);
			item.setSim_id(sim_id);
			deviceSimDao.save(item);
			simDao.updateSimInstall(sim_id,1);
			return true;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	
	//删除 设备-sim卡关系,操作成功后 更新一下sim卡的安装状态  未安装
	public boolean deleteDeviceSim(int device_id, int sim_id) {
		try {
			DeviceSim item = deviceSimDao.getObject(device_id, sim_id);
			if(item!=null) {
				deviceSimDao.delete(item);
				simDao.updateSimInstall(sim_id,0);
				return true;
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
